<script setup lang="ts">
import type { AdItemType, TopicType, CommonType } from "@/type/lidare"
import { getMineAdList_l, getMineTags_l } from "@/api/lidare"
import { useRouter } from "vue-router"
import { ref, reactive, onMounted } from 'vue'
import { useUserInfo, useCounterStore } from "@/stores/counter"
import { storeToRefs } from "pinia"
const counterStore = useCounterStore();
const { collectData, phoneHistoryData } = storeToRefs(counterStore);
const store = useUserInfo();
const { isLogin, user } = storeToRefs(store);
const { changeIsLogin } = store;
const router = useRouter();
const adList = ref<Array<AdItemType>>([]);
const allTags = reactive<{
  topic: Array<TopicType>,
  common: Array<CommonType>
}>({ topic: [], common: [] });
const catIsLogin = () => {
  let token = localStorage.getItem("token") || false;
  if (!token) {
    router.push("/login");
  }
}
onMounted(async () => {
  let res = await getMineAdList_l();
  adList.value = res.data._data.adList;
  let res2 = await getMineTags_l();
  console.log(res2);
  allTags.topic = res2.data._data.info.topic;
  allTags.common = res2.data._data.info.common;

});
const toDetail = (to:string) => {
  routerTo(to);
};
const tagClick = (tag:{tag:string,name:string,picture:string})=>{
  switch(tag.name){
    case "收货地址":
    routerTo('/address');
    break;
  }
}
const routerTo = (to: string) => {
  if (!isLogin.value) {
    router.push('/login');
  } else {
    router.push(to);
  }
}
</script>
<template>
  <div class="mine">
    <header class="header">
      <div class="user" @click.stop="catIsLogin">
        <div class="img-box">
          <img src="@/assets/images/default-user.png" alt="">
        </div>
        <div class="login" v-if="!isLogin">
          登录/注册
        </div>
        <div class="login" v-else>卡布奇诺</div>
      </div>
      <div class="header-tags">
        <div class="header-tag" @click.stop="toDetail('/mycollect')">
          <div class="count">{{ collectData.length || 0 }}</div>
          <div class="tag-text">收藏夹</div>
        </div>
        <div class="header-tag" @click.stop="toDetail('/myhistory')">
          <div class="count">{{ phoneHistoryData.length || 0 }}</div>
          <div class="tag-text">足迹</div>
        </div>
        <div class="header-tag">
          <div class="count">0</div>
          <div class="tag-text">红包卡券</div>
        </div>
        <div class="header-tag">
          <div class="count">0</div>
          <div class="tag-text">钱包</div>
        </div>
      </div>
    </header>
    <div class="order" @click.stop="toDetail('/order')">
      <div class="order-title">
        <div class="order-title-text">我的订单</div>
        <div class="order-title-more"><span>全部订单</span><van-icon class="icon" name="arrow" /></div>
      </div>
      <div class="img-box order-tags">
        <div class="bgi"></div>
        <div class="order-tag">待付款</div>
        <div class="order-tag">待收货</div>
        <div class="order-tag">评价有礼</div>
        <div class="order-tag">退换售后</div>
        <div class="order-tag">回收订单</div>
      </div>
    </div>
    <div class="ad">
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white" :show-indicators='false'>
        <van-swipe-item v-for="(item, index) in adList" class="img-box">
          <img :src="item.content" alt="">
        </van-swipe-item>
      </van-swipe>
    </div>
    <div class="topic">
      <div class="topic-item" v-for="(topic, index) in allTags.topic" :key="index">
        <div class="img-box">
          <img :src="topic.picture" alt="">
          <div class="tag" v-if="topic.tag">{{ topic.tag }}</div>
        </div>
        <div class="right">
          <div class="title">{{ topic.name }}</div>
          <div class="des">{{ topic.description }}</div>
        </div>
      </div>
    </div>
    <div class="common">
      <div class="common-title">常用功能</div>
      <div class="common-tags">
        <div class="common-tag" v-for="(tag, index) in allTags.common" :key="index" @click.stop="tagClick(tag)">
          <div class="img-box">
            <div class="tag">{{ tag.tag }}</div>
            <img :src="tag.picture" alt="">
          </div>
          <div class="title">{{ tag.name }}</div>
        </div>
      </div>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.mine {
  width: 100%;
  box-sizing: border-box;
  height: calc(100vh - .5rem);
  background-color: var(--bgc-gray);

  .header {
    width: 100%;
    height: 2.1rem;
    background-image: url(@/assets/images/mine/bgi.png);
    background-size: contain;
    background-repeat: repeat-y;
    padding: 0 .2rem;
    padding-top: .2rem;
    box-sizing: border-box;

    .user {
      width: 100%;
      display: flex;
      align-items: center;
      margin-top: .1rem;

      .img-box {
        width: .62rem;
        min-width: .62rem;
        height: .62rem;
        border-radius: 50%;
        overflow: hidden;
      }

      .login {
        color: var(--text-white);
        margin-left: .1rem;
        font-size: .18rem;
        opacity: .9;
      }
    }

    .header-tags {
      width: 100%;
      background-color: transparent;
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: .2rem;

      .header-tag {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: var(--text-white);

        .count {
          font-size: .13rem;
          font-weight: bold;
        }

        .tag-text {
          margin-top: .02rem;
          font-size: .12rem;
          opacity: .8;
        }
      }
    }
  }

  .order {
    width: 3.5rem;
    height: 1.08rem;
    margin: 0 auto;
    margin-top: -.32rem;
    border-radius: .1rem;
    background-color: var(--bgc-white);
    box-sizing: border-box;
    padding: .1rem;

    .order-title {
      width: 100%;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .order-title-text {
        font-size: .17rem;
        font-weight: bold;
        color: var(--text-black);
      }

      .order-title-more {
        font-size: .13rem;
        color: var(--text-gray);
      }
    }

    .order-tags {
      position: relative;
      width: 100%;
      height: .67rem;
      margin-top: .1rem;
      display: flex;
      justify-content: space-between;
      align-items: flex-end;

      .bgi {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: .3rem;
        background-image: url(@/assets/images/mine/tags.png);
        background-size: contain;
        z-index: 0;
      }

      .order-tag {
        position: relative;
        flex: 1;
        font-size: .12rem;
        color: var(--text-gray);
        text-align: center;
        margin-bottom: .15rem;
      }
    }
  }

  .ad {
    width: 3.5rem;
    height: .72rem;
    margin: 0 auto;
    margin-top: .1rem;
    border-radius: .1rem;
    overflow: hidden;
    background-color: var(--bgc-white);
  }

  .topic {
    width: 3.5rem;
    height: .68rem;
    border-radius: .1rem;
    margin: 0 auto;
    margin-top: .1rem;
    overflow: hidden;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .topic-item {
      width: 1.73rem;
      height: 100%;
      background-color: var(--bgc-white);
      display: flex;
      justify-content: center;
      align-items: center;

      .img-box {
        position: relative;
        width: .5rem;
        height: .5rem;

        .tag {
          position: absolute;
          right: 0;
          top: 0;
          height: .13rem;
          padding: .02rem;
          background-color: var(--bgc-red);
          color: var(--text-white);
          font-size: .12rem;
          line-height: .15rem;
          border-radius: .02rem;
          transform: scale(.6);
        }
      }

      .right {
        .title {
          font-size: .15rem;
          font-weight: bold;
        }

        .des {
          font-size: .12rem;
          color: var(--text-gray);
        }
      }
    }
  }

  .common {
    width: 3.5rem;
    background-color: var(--bgc-white);
    box-sizing: border-box;
    margin: 0 auto;
    margin-top: .1rem;
    border-radius: .1rem;
    overflow: hidden;
    padding: .1rem;

    .common-title {
      font-size: .15rem;
      font-weight: bold;
    }

    .common-tags {
      width: 100%;
      display: flex;
      flex-wrap: wrap;

      .common-tag {
        width: 25%;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;

        .img-box {
          position: relative;
          width: .4rem;
          aspect-ratio: 1/1;

          .tag {
            position: absolute;
            right: 0;
            top: 0.04rem;
            width: max-content;
            font-size: .12rem;
            color: var(--text-white);
            background-color: var(--bgc-red);
            background-image: linear-gradient(to right, var(--bgc-red), var(--bgc-red-deep));
            border-radius: .02rem;
            padding: 0 0.02rem;
            transform-origin: 100% 0 0;
            transform: scale(.5);
          }
        }

        .title {
          font-size: .13rem;
          font-weight: bold;
          opacity: .8;
        }
      }
    }
  }
}
</style>